﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Ajax : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["M"] == null || Session["user"] == null)
        {
            Response.Write("{\"Status\":\"Error\"}");
            Response.End();
        }

        switch(Request.Params["M"]){
            case "HoanTat":
                HoanTat();
                break;
            case "LuuDapAn":
                LuuDapAn();
                break;
            case "GetBaiThi":
                GetBaiThi();
                break;
            case "DoiMatKhau":
                DoiMatKhau();
                break;
        }
    }

    private void HoanTat() {
        if (Request.Params["Data"] == null || Request.Params["DeThi"] == null)
        {
            Response.Write("{\"Status\":\"Error\"}");
            Response.End();
        }
        JavaScriptSerializer serial = new JavaScriptSerializer();
        string data = Request.Params["Data"].ToString();
        int MaDe = int.Parse(Request.Params["DeThi"]);
        int MaBaiThi = int.Parse(Session["BaiThi"].ToString());
        List<TracNghiemJSON> dsbailam = serial.Deserialize<List<TracNghiemJSON>>(data);

        DeThiINFO mDeThi = DeThiDAL.GetInfo(MaDe);
        double diem = mDeThi.ThangDiem / mDeThi.SoCauHoi;
        double tongdiem = 0;
        BaiThiINFO mBaiThi = BaiThiDAL.getInfo(MaBaiThi);
        List<ChiTietBaiThiINFO> bailam = ChiTietBaiThiDAL.listAll(MaBaiThi);

        foreach (var item in dsbailam)
        {
            List<string> dsDA = DapAnDAL.kiemtra(item.CauHoi);
            var dsChon = item.TraLoi.Split(',');
            int dem = 0;
            for (int i = 0; i < dsChon.Length; i++)
            {
                if (dsDA.IndexOf(dsChon[i]) > -1) {
                    dem++;
                }
            }
            ChiTietBaiThiINFO obj = new ChiTietBaiThiINFO();
            obj.DapAnChon = item.TraLoi;
            obj.MaCH = item.CauHoi;
            if (dem == dsDA.Count)
            {
                obj.GiaTri = 1;
                tongdiem += diem;
            }
            else
            {
                obj.GiaTri = 0;
            }
            if(bailam.Find(t => t.MaCH == item.CauHoi) != null){
                bailam.Find(t => t.MaCH == item.CauHoi).DapAnChon = obj.DapAnChon;
                bailam.Find(t => t.MaCH == item.CauHoi).GiaTri = obj.GiaTri;
            }else{
                bailam.Add(obj);
            }
        }

        mBaiThi.Diem = tongdiem;
        mBaiThi.TinhTrang = 3;
        mBaiThi.ThoiGianKetThuc = DateTime.Now;
        mBaiThi.ThoiGianThi = 0;

        if (BaiThiDAL.HoanTatBaiThi(mBaiThi, bailam) > 0)
        {
            Session.Remove("BaiThi");
            Response.Write("{\"Status\":\"Success\"}");
        }
        else {
            Response.Write("{\"Status\":\"Error\"}");
        }
        
        Response.End();
    }

    private void LuuDapAn()
    {
        if (Request.Params["Data"] == null || Request.Params["DeThi"] == null)
        {
            Response.Write("{\"Status\":\"Error\"}");
            Response.End();
        }
        JavaScriptSerializer serial = new JavaScriptSerializer();
        string data = Request.Params["Data"].ToString();
        int MaDe = int.Parse(Request.Params["DeThi"]);
        TracNghiemJSON dsbailam = serial.Deserialize<TracNghiemJSON>(data);

        DeThiINFO mDeThi = DeThiDAL.GetInfo(MaDe);
        double diem = mDeThi.ThangDiem / mDeThi.SoCauHoi;
        int MaBaiThi = int.Parse(Session["BaiThi"].ToString());
        BaiThiINFO mBaiThi = BaiThiDAL.getInfo(MaBaiThi);
        double tongdiem = mBaiThi.Diem;

        ChiTietBaiThiINFO obj = ChiTietBaiThiDAL.getInfo(MaBaiThi, dsbailam.CauHoi);

        List<string> dsDA = DapAnDAL.kiemtra(dsbailam.CauHoi);
        var dsChon = dsbailam.TraLoi.Split(',');
        int dem = 0;
        for (int i = 0; i < dsChon.Length; i++)
        {
            if (dsDA.IndexOf(dsChon[i]) > -1)
            {
                dem++;
            }
        }
        if (obj != null || obj.MaChiTiet > 0) {
            if (obj.GiaTri == 1)
            {
                tongdiem -= diem;
            }
        }
        obj.DapAnChon = dsbailam.TraLoi;
        obj.MaCH = dsbailam.CauHoi;
        obj.MaBaiThi = mBaiThi.MaBaiThi;
        if (dem == dsDA.Count)
        {
            obj.GiaTri = 1;
            tongdiem += diem;
        }
        else
        {
            obj.GiaTri = 0;
        }
        mBaiThi.Diem = tongdiem;
        DateTime cur = DateTime.Now;
        int m = cur.Subtract(mBaiThi.ThoiGianKetThuc).Minutes;
        int s = cur.Subtract(mBaiThi.ThoiGianKetThuc).Seconds;
        double tongthoigian = mBaiThi.ThoiGianThi - ((m * 60) + s);
        mBaiThi.ThoiGianKetThuc = cur;
        mBaiThi.ThoiGianThi = (int) tongthoigian;
        if (BaiThiDAL.LuuDapAn(mBaiThi, obj) > 0)
        {
            Response.Write("{\"Status\":\"Success\"}");
        }
        else
        {
            Response.Write("{\"Status\":\"Error\"}");
        }
        Response.End();
    }

    private void GetBaiThi()
    {
        if (Request.Params["Ma"] == null)
        {
            Response.Write("{\"Status\":\"Error\"}");
            Response.End();
        }
        int MaDe = int.Parse(Request.Params["Ma"]);
        Session["BaiThi"] = MaDe;
        Response.Write("{\"Status\":\"Success\"}");
        Response.End();
    }

    private void DoiMatKhau()
    {
        if (Request.Params["MatKhau"] == null || Request.Params["MatKhauMoi"] == null)
        {
            Response.Write("{\"Status\":\"Error\",\"Mess\":\"Dữ liệu không đúng!\"}");
            Response.End();
        }
        int MaHV = int.Parse(Session["user"].ToString());
        string mkhientai = Request.Params["MatKhau"];
        string mkmoi = Request.Params["MatKhauMoi"];
        HocVienINFO mHocVien = HocVienDAL.GetInfo(MaHV);
        if (!mHocVien.MatKhau.Equals(mkhientai)) {
            Response.Write("{\"Status\":\"Error\",\"Mess\":\"Mật khẩu hiện tại không đúng!\"}");
            Response.End();
        }
        if (HocVienDAL.DoiMatKhau(mkmoi, MaHV) > 0)
        {
            Response.Write("{\"Status\":\"Success\"}");
        }
        else {
            Response.Write("{\"Status\":\"Error\",\"Mess\":\"Không thể đổi mật khẩu!\"}");        }
        Response.End();
    }
}